package com.wbt.bambootown.ui.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.ControlDevices;
import com.wbt.bambootown.mvp.contract.ControlDevicesContract;
import com.wbt.bambootown.mvp.frame.MvpFragment;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.ControlDevicesPresenter;
import com.wbt.bambootown.ui.activity.ControlLogActivity;
import com.wbt.bambootown.ui.adapter.DevicesControlAdapter;
import com.wbt.bambootown.ui.adapter.DevicesDataAdapter;
import com.wbt.bambootown.utils.EventCenter;
import java.util.List;

import butterknife.BindView;


/**
 * Created by wufan on 2018/5/15.
 */

public class RemoteControlFragment extends MvpFragment<ControlDevicesPresenter> implements ControlDevicesContract.View, DevicesControlAdapter.onControlClick {
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.rv_devices)
    RecyclerView mRvDevices;

    private DevicesControlAdapter adapter;

    @Override
    protected boolean userDefaultStatus() {
        return true;
    }

    @Override
    protected void initView() {
        refreshLayout.setColorSchemeResources(R.color.theme_color);
        //给swipeRefreshLayout绑定刷新监听
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getControlDevices(FrameApp.currentFacilityId);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new DevicesControlAdapter(getContext());
        mRvDevices.setLayoutManager(layoutManager);
        mRvDevices.setAdapter(adapter);
        adapter.setListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {
        mPresenter.getControlDevices(FrameApp.currentFacilityId);
        showProgressDialog();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {

    }

    @Override
    public int getContentView() {
        return R.layout.fragment_remotecontrol;
    }

    @Override
    protected boolean isApplyEventBus() {
        return true;
    }

    @Override
    protected boolean isApplyButterKnife() {
        return true;
    }

    @Override
    protected void onEventCome(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == Cons.Event.EVENT_CONTROL_DEVICES) {
            mPresenter.getControlDevices(FrameApp.currentFacilityId);
        }
        if(eventCenter.getEventCode() == Cons.DevicesControl.DEVICES_CONTROL_SUCCESS){
            mPresenter.getControlDevices(FrameApp.currentFacilityId);
            Toast.makeText(getContext() , "控制成功" , Toast.LENGTH_SHORT).show();
        }else if(eventCenter.getEventCode() == Cons.DevicesControl.DEVICES_CONTROL_FAILED){
            Toast.makeText(getContext() , "控制失败" , Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        refreshLayout.setRefreshing(false);
        Toast.makeText(getContext() , errorMsg , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDevicesSuccess(List<ControlDevices> controlDevices) {
        dismissProgressDialog();
        refreshLayout.setRefreshing(false);
        adapter.setList(controlDevices);
    }


    @Override
    public void cmdType(int id, int type) {
        if(type == -1){
            //查询控制记录
            Intent mIntent = new Intent(getContext() , ControlLogActivity.class);
            mIntent.putExtra(Cons.SPCons.LIGHT_DEVICES_ID , id);
            startActivity(mIntent);
        }else{
            FrameApp.mService.sendToWindowsServer(id , type);
        }
    }
}
